import 'package:intl/intl.dart';

extension DateTimeExtension on DateTime {
  /// 转换为时间戳（毫秒）
  int get timestamp => millisecondsSinceEpoch;

  /// 转换为时间戳字符串
  String get timestampString => timestamp.toString();

  /// 转换为标准格式字符串 (yyyy-MM-dd HH:mm:ss)
  String get standardString => DateFormat('yyyy-MM-dd HH:mm:ss').format(this);

  /// 转换为日期字符串 (yyyy-MM-dd)
  String get dateString => DateFormat('yyyy-MM-dd').format(this);

  /// 转换为时间字符串 (HH:mm:ss)
  String get timeString => DateFormat('HH:mm:ss').format(this);

  /// 转换为短时间字符串 (HH:mm)
  String get shortTimeString => DateFormat('HH:mm').format(this);

  /// 转换为年月字符串 (yyyy-MM)
  String get yearMonthString => DateFormat('yyyy-MM').format(this);

  /// 转换为月日字符串 (MM-dd)
  String get monthDayString => DateFormat('MM-dd').format(this);

  /// 转换为中文日期字符串 (yyyy年MM月dd日)
  String get chineseDateString => DateFormat('yyyy年MM月dd日').format(this);

  /// 转换为中文日期时间字符串 (yyyy年MM月dd日 HH:mm:ss)
  String get chineseDateTimeString => DateFormat('yyyy年MM月dd日 HH:mm:ss').format(this);

  /// 转换为相对时间字符串（例如：刚刚、x分钟前、x小时前、昨天、x天前）
  String get relativeTimeString {
    final now = DateTime.now();
    final difference = now.difference(this);

    if (difference.inSeconds < 60) {
      return '刚刚';
    } else if (difference.inMinutes < 60) {
      return '${difference.inMinutes}分钟前';
    } else if (difference.inHours < 24) {
      return '${difference.inHours}小时前';
    } else if (difference.inDays == 1) {
      return '昨天';
    } else if (difference.inDays < 7) {
      return '${difference.inDays}天前';
    } else {
      return dateString;
    }
  }
}

extension StringDateTimeExtension on String {
  /// 从时间戳字符串转换为DateTime
  DateTime? get fromTimestamp {
    try {
      return DateTime.fromMillisecondsSinceEpoch(int.parse(this));
    } catch (e) {
      return null;
    }
  }

  /// 从标准格式字符串转换为DateTime (yyyy-MM-dd HH:mm:ss)
  DateTime? get fromStandardString {
    try {
      return DateFormat('yyyy-MM-dd HH:mm:ss').parse(this);
    } catch (e) {
      return null;
    }
  }

  /// 从日期字符串转换为DateTime (yyyy-MM-dd)
  DateTime? get fromDateString {
    try {
      return DateFormat('yyyy-MM-dd').parse(this);
    } catch (e) {
      return null;
    }
  }

  /// 从时间字符串转换为DateTime (HH:mm:ss)
  DateTime? get fromTimeString {
    try {
      final now = DateTime.now();
      final time = DateFormat('HH:mm:ss').parse(this);
      return DateTime(
        now.year,
        now.month,
        now.day,
        time.hour,
        time.minute,
        time.second,
      );
    } catch (e) {
      return null;
    }
  }

  /// 从年月字符串转换为DateTime (yyyy-MM)
  DateTime? get fromYearMonthString {
    try {
      return DateFormat('yyyy-MM').parse(this);
    } catch (e) {
      return null;
    }
  }

  /// 从年月字符串转换为DateTime (yyyy-MM)
  DateTime? get fromMonthDayString {
    try {
      return DateFormat('MM-dd').parse(this);
    } catch (e) {
      return null;
    }
  }

  /// 从中文日期字符串转换为DateTime (yyyy年MM月dd日)
  DateTime? get fromChineseDateString {
    try {
      return DateFormat('yyyy年MM月dd日').parse(this);
    } catch (e) {
      return null;
    }
  }

  /// 从中文日期时间字符串转换为DateTime (yyyy年MM月dd日 HH:mm:ss)
  DateTime? get fromChineseDateTimeString {
    try {
      return DateFormat('yyyy年MM月dd日 HH:mm:ss').parse(this);
    } catch (e) {
      return null;
    }
  }

  /// 转换为时间戳字符串
  String? get toTimestampString {
    final dateTime = fromStandardString ?? 
                    fromDateString ?? 
                    fromTimeString ?? 
                    fromYearMonthString ?? 
                    fromChineseDateString ?? 
                    fromChineseDateTimeString;
    return dateTime?.timestampString;
  }
}

/// 时间格式常量
class DateTimeFormat {
  static const String standard = 'yyyy-MM-dd HH:mm:ss';
  static const String date = 'yyyy-MM-dd';
  static const String time = 'HH:mm:ss';
  static const String shortTime = 'HH:mm';
  static const String yearMonth = 'yyyy-MM';
  static const String chineseDate = 'yyyy年MM月dd日';
  static const String chineseDateTime = 'yyyy年MM月dd日 HH:mm:ss';
} 